<template>
  <div class="tag-rules">
    <el-input
      placeholder="点击选择规则"
      v-model="showRuleName"
      readonly
      :disabled="!editable"
      @click.native="openDialog"
      :size="inputSize"
      ><i slot="suffix" v-if="editable" class="el-input__icon el-icon-close" @click.stop="clearSelection"></i
    ></el-input>
    <el-dialog
      :visible.sync="dialogVisible"
      top="10vh"
      append-to-body
      :close-on-click-modal="false"
      class="tag-edit-dialog tag-rules-dialog"
      width="50%"
      @closed="clearDialog"
    >
      <div slot="title">
        <tag-content-header>
          <template #title>
            <span class="title-text">规则列表</span>
          </template>
        </tag-content-header>
      </div>

      <div class="search-box">
        <el-input v-model="searchKey" size="small" placeholder="搜索规则">
          <i slot="suffix" class="el-input__icon el-icon-close" @click="searchKey = ''"></i>
        </el-input>
        <div class="btn-box">
          <el-button type="primary" icon="el-icon-plus" plain circle @click="handleEdit('add')"></el-button>
        </div>
      </div>

      <div class="item-box tag-scrollbar" v-loading="loading">
        <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="checkAllChange" class="check-all">
          全选
        </el-checkbox>
        <el-checkbox-group v-model="checkId" @change="checkChange">
          <div class="item-row" v-for="item in ruleList" :key="item.tmplId">
            <el-checkbox :label="item.tmplId">{{ item.ruleNm }}</el-checkbox>
            <div class="item-btn">
              <el-tooltip effect="dark" content="编辑" placement="top" :enterable="false">
                <i class="el-icon-edit-outline" @click.stop="handleEdit('edit', item)"></i>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                <i class="el-icon-delete" @click.stop="handleDelete(item)"></i>
              </el-tooltip>
            </div>
          </div>
        </el-checkbox-group>
      </div>
      <edit-dialog ref="editDialog" @refresh="getList"></edit-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取消</el-button>
        <el-button type="primary" @click="submit" :disabled="!checkId">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import editDialog from './tag-rules-edit'
export default {
  name: 'tag-rules',
  components: {
    editDialog
  },
  props: {
    ruleId: {
      default: () => {
        return []
      }
    },
    ruleName: {
      default: () => {
        return []
      }
    },
    editable: {
      type: Boolean,
      default: true
    },
    inputSize: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      firstopen: true,
      dialogVisible: false,
      loading: false,
      searchKey: '',
      dataList: [],
      dataIds: {},
      checkId: [],
      selectname: [],
      checkAll: false,
      isIndeterminate: true
    }
  },
  computed: {
    ruleList() {
      if (!this.searchKey) return this.dataList
      return this.dataList.filter(item => {
        return item.ruleNm.indexOf(this.searchKey) !== -1
      })
    },
    showRuleName() {
      return this.ruleName.join(',')
    }
  },
  methods: {
    openDialog() {
      if (this.editable) {
        this.checkId = this.ruleId
        this.getList()
        this.dialogVisible = true
      }
    },
    clearSelection() {
      this.$emit('confirm', {
        id: [],
        name: []
      })
    },
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('sys/sysUserRuleTmpl'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.dataList = data.data
          this.setDataIds(data.data)
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    checkAllChange(val) {
      let checkId = Object.keys(this.dataIds)
      for (let i = 0; i < checkId.length; i++) {
        checkId[i] = Number(checkId[i])
      }
      this.checkId = val ? checkId : []
      this.isIndeterminate = false
    },
    checkChange(value) {
      let checkedCount = value.length
      let checkId = Object.keys(this.dataIds)
      for (let i = 0; i < checkId.length; i++) {
        checkId[i] = Number(checkId[i])
      }
      this.checkAll = checkedCount === checkId.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < checkId.length
    },
    setDataIds(data) {
      var ids = {}
      data.forEach(item => {
        ids[item.tmplId] = item.ruleNm
      })
      this.dataIds = ids
    },
    handleEdit(type = 'add', item) {
      if (type === 'add') {
        this.$refs.editDialog.showDialog({
          type: 'add'
        })
      } else {
        this.getDetail(item)
      }
    },
    getDetail(item) {
      this.$ajax({
        url: this.$ajax.setUrl('sys/sysUserRuleConfig/' + item.tmplId),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.$refs.editDialog.showDialog({
            type: 'edit',
            list: data.data,
            name: item.ruleNm,
            id: item.tmplId
          })
        } else {
          this.$message.error(data.data || data.msg)
        }
      })
    },
    handleDelete(item) {
      this.$confirm('确认删除【' + item.ruleNm + '】？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl('sys/sysUserRuleTmpl/' + item.tmplId),
            method: 'delete',
            params: this.$ajax.setParams()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              if (this.checkId === item.tmplId) this.checkId = ''
              this.getList()
              this.$message.success(data.data || data.msg)
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {})
    },
    submit() {
      this.selectname = []
      for (var i = 0; i < this.checkId.length; i++) {
        this.selectname.push(this.dataIds[this.checkId[i]])
      }
      this.$emit('confirm', {
        id: this.checkId,
        name: this.selectname
      })
      this.dialogVisible = false
    },
    clearDialog() {
      this.loading = false
      this.searchKey = ''
      this.dataList = []
      this.dataIds = {}
      this.checkId = []
    },
    cancle() {
      this.dialogVisible = false
      this.clearDialog()
    }
  }
}
</script>

<style lang="scss">
.tag-rules {
  width: 100%;
  .el-input__inner,
  .el-input.is-disabled .el-input__inner {
    cursor: pointer;
  }
  .el-input__inner {
    width: 100%;
    position: relative;
    i {
      position: absolute;
      top: 0;
      right: 0;
    }
  }
}
.tag-rules-dialog {
  .search-box {
    position: relative;
    padding-right: 44px;
    .el-input {
      width: 100%;
    }
    .btn-box {
      position: absolute;
      top: 4px;
      right: 10px;
      .el-button {
        padding: 4px;
      }
    }
  }
  .item-box {
    margin-top: 10px;
    border: 1px solid $border-color;
    border-radius: 4px;
    height: 300px;
    overflow: auto;
    &:hover {
      border-color: #c0c4cc;
    }
    .check-all {
      margin-top: 10px;
      margin-left: 10px;
    }
    .el-checkbox-group {
      width: 100%;
      .el-checkbox {
        width: 100%;
        line-height: 30px;
        margin-right: 0;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        .el-checkbox__label {
          padding-left: 4px;
        }
      }
    }
    .item-row {
      position: relative;
      padding-left: 10px;
      padding-right: 60px;
      &:hover {
        background-color: #f5f7fa;
        .item-btn {
          display: block;
        }
      }
      .item-btn {
        position: absolute;
        display: none;
        height: 30px;
        line-height: 30px;
        right: 0;
        top: 0;
        i {
          font-size: 16px;
          margin-right: 12px;
          line-height: inherit;
          cursor: pointer;
        }
        .el-icon-edit-outline {
          color: #ffb431;
        }
        .el-icon-delete {
          color: #f47d7d;
        }
      }
    }
  }
}
</style>
